package com.example.Windturbine.SimilarLearning.Listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.Windturbine.Controller.ViewObject.Excel.PartExcel;
import com.example.Windturbine.SimilarLearning.AutoAddDataUtil;
import com.example.Windturbine.SimilarLearning.LearningDO.Sample;
import com.example.Windturbine.SimilarLearning.LearningDO.TrainDO;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author 丁永新
 * @Date 2022/1/8
 */
@Data
public class SampleListener extends AnalysisEventListener<Sample> {

    List<TrainDO> trainDOS;

    public SampleListener(List<TrainDO> trainDOS){
        this.trainDOS=trainDOS;
    }

    @Override
    public void invoke(Sample sample, AnalysisContext analysisContext) {
        trainDOS.add(calTrainDO(sample));
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    //将Sample计算为Train
    public TrainDO calTrainDO(Sample sample){
        AutoAddDataUtil util=new AutoAddDataUtil();
        double n1 = util.wordHandle(sample.getNameInfo(), sample.getName());
        double n2 = util.wordHandle(sample.getSerialInfo(), sample.getSerial());
        double n3 = util.wordHandle(sample.getFunctionInfo(), sample.getFunction());
        double n4 = util.wordHandle(sample.getOriginInfo(), sample.getOrigin());
        double n5 = util.wordHandle(sample.getParameterInfo(), sample.getParameter());
        double n6 = util.wordHandle(sample.getMaterialInfo(), sample.getMaterial());
        double n7 = util.numHandle(sample.getWeighttotalInfo(), sample.getWeighttotal());
        double n8 = util.wordHandle(sample.getTransportstyleInfo(), sample.getTransportstyle());
        double n9 = util.numHandle(new BigDecimal(sample.getDistanceInfo()), new BigDecimal(sample.getDistance()));
        double n10 = util.wordHandle(sample.getManufacturestyleInfo(), sample.getManufacturestyle());
        double n11 = util.numHandle(new BigDecimal(sample.getRecycleInfo()), new BigDecimal(sample.getRecycle()));
        String res=sample.getResult();
        Double yes=res.equals("Y")?1.0:0.0;
        Double no=res.equals("N")?1.0:0.0;
        TrainDO trainDO=new TrainDO(n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,yes,no);
        return  trainDO;
    }
}
